package com.hypo.hystrixservice.command;

import com.alibaba.fastjson.JSONObject;
import com.hypo.hystrixservice.domain.UserInfo;
import com.hypo.hystrixservice.http.HttpClientUtils;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand;
import rx.Observable;

import java.util.List;

/**
 * @Author: haibo.tang2
 * @Date: 2021/11/16
 * @Description:
 **/
public class GetUserInfoListCommand extends HystrixObservableCommand<UserInfo> {
    private List<String> userIds;
    public GetUserInfoListCommand(List<String> userIds) {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.userIds = userIds;
    }

    @Override
    protected Observable<UserInfo> construct() {
        return Observable.create(subscriber -> {
            subscriber.onStart();
            for (int i = 0; i < userIds.size(); i++) {
                String url = "http://127.0.0.1:8082/user/getUserInfo?userId=" + userIds.get(i);
                String response = HttpClientUtils.sendGetRequest(url);
                UserInfo userInfo = JSONObject.parseObject(response, UserInfo.class);
                subscriber.onNext(userInfo);
            }
            subscriber.onCompleted();
        });
    }
}
